'use strict';

var services = angular.module('guthub.services', ['ngResource']);

services.factory('Patient', ['$resource',
  function($resource) {
    return $resource('/apps/services/patient/patientid/:id', {
      id: '@id'
    });
  }
]);

services.factory('MultiPatientLoader', ['Patient', '$q',
  function(Patient, $q) {
    return function() {
      var delay = $q.defer();
      Patient.query(function(patients) {
        delay.resolve(patients);
      }, function() {
        delay.reject('Unable to fetch recipes');
      });
      return delay.promise;
    };
  }
]);

services.factory('PatientLoader', ['Patient', '$route', '$q',
  function(Patient, $route, $q) {
    return function() {
      var delay = $q.defer();
      Patient.get({
        id: $route.current.params.patientId
      }, function(patient) {
        delay.resolve(patient);
      }, function() {
        delay.reject('Unable to fetch recipe ' + $route.current.params.patientId);
      });
      return delay.promise;
    };
  }
]);

services.factory('Photos', ['$resource', '$route',
  function($resource, $route) {
    return $resource('/apps/services/photo/patientid/:id', {
      id: '@id'
    });
  }
]);


services.factory('MultiPhotoLoader', ['Photos', '$q', '$route',
  function(Photos, $q, $route) {
    return function() {
      var delay = $q.defer();
      Photos.query({
        id: $route.current.params.patientId
      }, function(photos) {
        delay.resolve(photos);
      }, function() {
        delay.reject('Unable to fetch recipes');
      });
      return delay.promise;
    };
  }
]);
